package com.carpedil.proxy.dao.impl;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/** @author Administrator 1. 实现MethodInterceptor接口 */
public class MyInterceptor implements MethodInterceptor {
  private Object target;

  public MyInterceptor() {}

  public MyInterceptor(Object target) {
    this.target = target;
  }

  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
      throws Throwable {
    System.out.println("cgLib代理开始");
    // 可以通过构造函数传入具体需要代理的对象
    // Object invoke1 = method.invoke(target, objects);
    // 通过代理方法直接找到需要代理的对象
    Object invoke = methodProxy.invokeSuper(o, objects);
    System.out.println("cgLib代理结束");
    return invoke;
  }
}
